<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/13
 * Time: 16:34
 */
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\models\DwDataJobsSearch;
use yii\filters\VerbFilter;

class SearchController extends Controller
{
    public $enableCsrfValidation=false;
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        if(!Yii::$app->request->post()){
            return $this->redirect(['site/index']);
        }
        $searchModel = new DwDataJobsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->post())->models;

        $resultData = array();
        foreach($dataProvider as $list){
            $sql = "select t2.job_name,t3.city_name max_city_name,t4.city_name min_city_name,t1.* from t_data_job_list t1 LEFT JOIN dw_data_jobs t2 on t1.job_id=t2.id LEFT JOIN dw_data_city t3 on t1.max_city_id =t3.id LEFT JOIN dw_data_city t4 on t1.min_city_id =t4.id  where job_id='{$list->id}' order by date_line desc limit 1;
";
            $row = Yii::$app->db->createCommand($sql)->queryAll();
            if($row){
                $resultData = array_merge($row,$resultData);
            }
        }
        return $this->render('index', [
            'resultData' => $resultData
        ]);
    }

    public function actionMoredesc()
    {
        return $this->render('moredesc');
    }
}
